<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--公寓楼名称-->
      <el-col :span="24" :xs="24">
        <!-- 搜索 -->
        <searchForm v-if="!houseId" :formOptions="formOptions" @onSearch="onSearch" @onReset="onReset"
          :formLabelWidth="formLabelWidth" v-show="showSearch" />
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['dorm:floor:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <!-- <el-button type="warning" size="mini">一键清空</el-button> -->
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table @cell-dblclick="tableClick" :data="tablelist" @selection-change="handleSelectionChange"  v-loading="loading">
          <el-table-column label="公寓楼名称" prop="houseName" :show-overflow-tooltip="true" align="center" />
          <el-table-column label="楼层号" prop="floorNo" align="center" />
          <!-- <el-table-column label="公寓楼房间数" prop="totalRoom" align="center" /> -->
          <!-- <el-table-column label="负责人" prop="roleSort" align="center" />
          <el-table-column label="负责人电话" prop="roleSort" align="center" />
          <el-table-column label="公寓楼简称" prop="roleSort" align="center" /> -->
          <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text"  @click="handleUpdate(scope.row)" v-hasPermi="['dorm:floor:edit']">修改</el-button>
              <el-button size="mini" type="text"  @click="handleDelete(scope.row)" v-hasPermi="['dorm:floor:remove']">删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
          @pagination="getList" />

        <!-- 不通过填写审核意见 -->
        <!-- 添加或修改角色配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="30%" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="110px">
            <el-row>
              <el-col :span="24">
                <el-form-item label="公寓楼名称" prop="houseId">
                  <el-select v-model="form.houseId" :disabled="$route.query.houseId" placeholder="请输入公寓楼名称"
                    style="width: 100%">
                    <el-option v-for="dict in houseList" :key="dict.houseId" :label="dict.houseName"
                      :value="dict.houseId">
                    </el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="楼层编号" prop="floorNo">
                  <el-input placeholder="请输入楼层编号" v-model="form.floorNo" maxlength="30" />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="公寓楼房间数" prop="totalRoom">
                  <el-input type="number" max="20" v-model="form.totalRoom" placeholder="请输入公寓楼房间数" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="open = false">取 消</el-button>
          </div>
        </el-dialog>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { mapState, mapActions } from 'vuex'
import searchForm from "@/components/Search/searchForm.vue";
import leftHouseTree from './leftHouseTree.vue';

export default {
  components: { searchForm, leftHouseTree },
  data() {
    return {
      formLabelWidth: "70px",
      formOptions: [
        {
          label: "楼号",
          prop: "houseId",
          element: "el-select",
          options: [
          ],
          placeholder: "请输入楼号",
        },
        {
          label: "层号",
          prop: "floorNo",
          element: "el-input",
          placeholder: "请输入层号",
        },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        floorNo: undefined,
        houseId: undefined
      },
      // 表单参数
      form: {
        houseId: '',
        floorNo: "",
        totalRoom: ''
      },
      defaultProps: {
        children: "children",
        label: "label",
      },
      // 表单校验
      rules: {
        floorNo: [
          { required: true, message: "公寓楼层编号不能为空", trigger: "blur" },
        ],
        totalRoom: [
          { required: true, message: "总房间数量不能为空", trigger: "blur" },
        ],
      },
    };
  },

  props: {
    houseId: {
      type: String
    },
    noClick:Boolean
  },
  computed: {
    ...mapState({
      tablelist: state => state.apartmentFloor.list,
      houseList: state => state.apartmentBuilding.list,
    })
  },
  watch: {
    houseId: function () {
      this.queryParams.houseId = this.$route.query.houseId || this.houseId
      this.getList();
    }
  },

  async created() {
    await this.getHouseList()
    this.houseList.map(res => {
      this.formOptions[0].options.push({ label: res.houseName, value: res.houseId },)
    })
    this.queryParams.houseId = this.$route.query.houseId || this.houseId

    this.getList();

  },
  methods: {
    ...mapActions({
      getTableList: 'apartmentFloor/getTable',
      getHouseList: 'apartmentBuilding/getTable',
      getAllUser: 'getAllUser',
      addInfo: 'apartmentFloor/addInfo',
      putInfoById: 'apartmentFloor/putInfoById',
      delInfoById: 'apartmentFloor/delInfoById',
      getAllRoom: 'apartmentBuilding/allInfolist',
    }),
    /** 查询角色列表 */
    getList() {
      this.loading = true;
      this.getTableList({ ...this.queryParams }).then(res=>{
        this.total = res.total
        this.loading = false
      })
    },
    // 获取搜索表单提交的数据
    onSearch(val) {
      // 查询条件和分页数据合并
      const arr = Object.assign(this.queryParams, val);
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 重置
    onReset(data) {
      this.queryParams.pageNum = 1;
      const arr = Object.assign(this.queryParams, data);
      this.getList();
    },
    // 所有菜单节点数据
    getMenuAllCheckedKeys() {
      // 目前被选中的菜单节点
      let checkedKeys = this.$refs.menu.getCheckedKeys();
      // 半选中的菜单节点
      let halfCheckedKeys = this.$refs.menu.getHalfCheckedKeys();
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      return checkedKeys;
    },
    // 表单重置
    reset() {
      this.form = this.$options.data().form
      this.form.houseId = Number(this.$route.query.houseId) || null
      // this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true;
      this.title = "添加公寓楼";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = JSON.parse(JSON.stringify(row))
      this.open = true;
      this.title = "修改信息";
    },

    /** 提交按钮 */
    submitForm: function async() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          if (!this.form.floorId) {
            const { data, code } = await this.addInfo(this.form)
            if (code == 200) {
              this.$modal.msgSuccess("添加成功");
              this.open = false;
              this.getList();
              this.getAllRoom()
            }
          } else {
            const { data, code } = await this.putInfoById(this.form)
            if (code == 200) {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.getAllRoom();
            }
          }

          this.reset()
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.floorId || this.ids;
      this.$modal
        .confirm('是否确认删除名称为"' + row.houseName + '"的数据项？')
        .then(async () => {
          const { data, code } = await this.delInfoById({ floorIds: ids })
          this.getList();
          this.getAllRoom();
          this.$modal.msgSuccess("删除成功");
        })

    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/role/export",
        {
          ...this.queryParams,
        },
        `role_${new Date().getTime()}.xlsx`
      );
    },


    // 楼栋被双击后 进入楼层表格

    tableClick(row) {
      if(this.noClick){
        return
      }
      this.$router.push({
        path: '/apartment/apartment/apartmentFloor',
        query: {
          houseId: row.houseId
        }
      })
    },


    // 表格选择后
    handleSelectionChange() {

    },

    // 楼栋被双击后 进入楼层表格
    tableClick(row) {
      this.$router.push({
        path: '/apartment/apartment/apartmentBuilding',
        query: {
          floorId: row.floorId,
          houseId: row.houseId
        }
      })
    }
  },
};
</script>
